package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.Service.CheckItemService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.vo.CheckItemVO;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增了一个检查项：{}",checkItem.getName());
        checkItemService.add(checkItem);
        return new Result(true,"新增检查项成功",null);
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(Integer id){
        log.info("删除了一个检查项,id为：{}",id);
        checkItemService.delete(id);
        return new Result(true,"删除检查项成功",null);
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param checkgroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        List<Integer> list = checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
        log.info("查到了检查项个数：{}",list.size());
        return new Result(true,"查询检查项成功",list);
    }

    /**
     * 编辑页面
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑完成，id为:",checkItem.getId());
        checkItemService.edit(checkItem);
        return new Result(true,"编辑检查项成功",null);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("查询页面成功！{}",queryPageBean);
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        CheckItem checkItem = new CheckItem();
        List<CheckItem> lists =  checkItemService.findAll(checkItem);
        log.info("查询成功，查询到{}个检查项",lists.size());
        return new Result(true,"查询检查项成功",lists);
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,"查询检查项成功",checkItem);
    }
}
